"""
Containers objects
"""

from game.gamesrc.objects.thing import Thing
from game.gamesrc.objects.lock import Lock
from game.gamesrc.utils import myutils

#
#Containers classes
#


class BasicContainer(Thing):
    """
    This class implements a basic container object

    """
    def at_object_creation(self):
        super(BasicContainer, self).at_object_creation()
        self.db.flags.append('CONTAINER')

    def return_appearance(self, pobject):
        """
        This is a convenient hook for a 'look'
        command to call.
        """

        string = super(BasicContainer, self).return_appearance(pobject)
        
        if hasattr(self, "is_closed") and self.is_closed:
            string += "\n{wE' chiuso%s.{n\n"
            if hasattr(self, "is_locked") and self.is_locked:
                string = string % " a chiave"
            else: string = string % ""
            return string

        exits = []
        users = []
        things = []
        for content in [con for con in self.contents if con.access(pobject, 'view')]:
            if content == pobject:
                continue
            name = content.name.capitalize()
            if content.destination:
                exits.append("{W(uscita){n %s" % name)
            elif content.has_player:
                users.append("{c(player){n %s" % name)
            else:
                things.append(name)

        things = list(myutils.tile_objects(things))

        if users or things or exits:
            string += "\n{wContenuto:\n{n"
            if things:
                string += "\n".join(things) + "\n"
            if users:
                string += "\n".join(users) + "\n"
            if exits:
                string += "\n".join(exits)
        return string


class LockContainer(BasicContainer, Lock):
    """
    This container object can be closed.
    """
    pass


